package com.yonyou.cyxdms.basedata.service.service.smstemplate.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.exception.ServiceBizException;
import com.yonyou.cyx.function.utils.common.CommonUtils;
import com.yonyou.cyxdms.basedata.client.domains.vo.SmsTemplateVO;
import com.yonyou.cyxdms.basedata.service.constant.BasedataCommonConstants;
import com.yonyou.cyxdms.basedata.service.constant.BasedataDictConstants;
import com.yonyou.cyxdms.basedata.service.dto.smstemplate.SmsTemplateDTO;
import com.yonyou.cyxdms.basedata.service.dto.smstemplate.SmsTemplatePageDTO;
import com.yonyou.cyxdms.basedata.service.entity.smstemplate.SmsTemplatePO;
import com.yonyou.cyxdms.basedata.service.repository.smstemplate.SmsTemplateMapper;
import com.yonyou.cyxdms.basedata.service.service.smstemplate.SmsTemplateService;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 短信模板 服务实现类
 * </p>
 *
 * @author dengqiang
 * @since 2019-09-19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SmsTemplateServiceImpl extends ServiceImpl<SmsTemplateMapper, SmsTemplatePO> implements SmsTemplateService {

    @Autowired
    SmsTemplateMapper smsTemplateMapper;

    /**
     * 短信模板分页查询
     *
     * @param page
     * @param vsStockAppDTO
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyxdms.basedata.service.vo.smstemplate.SmsTemplateVO>
     * @author dengqinag
     * @since 2019/9/19
     */
    @Override
    public IPage<SmsTemplateVO> qrySmsTemplatePage(Page<SmsTemplateVO> page, SmsTemplatePageDTO vsStockAppDTO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        Map<String, Object> queryCondition = vsStockAppDTO.toMaps();
        queryCondition.put("appId",loginInfoDto.getAppId());
        queryCondition.put("ownerCode",loginInfoDto.getOwnerCode());
        List<SmsTemplateVO> voList =  smsTemplateMapper.qrySmsTemplatePage(page,queryCondition);
        return page.setRecords(voList);
    }

    /**
     * 添加短信模板信息
     *
     * @param stDto
     * @return java.lang.Long
     * @author dengqiang
     * @since 2019/9/19
     */
    @Override
    public Long addSmsTemplate(SmsTemplateDTO stDto) {
        SmsTemplatePO stPo = new SmsTemplatePO();
        LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        searchBySmsTemplateDefault(stDto.getSmsTemplateType(),stDto.getIsDefault());
        stDto.setSmsTemplateContent(stDto.getSmsTemplateContent() + BasedataCommonConstants.SMS_TEMPLATE
                +"【" +loginInfo.getDealerName()+"】");
        setSmsTemplate(stPo, stDto);
        stPo.setAppId(loginInfo.getAppId());
        stPo.setCompanyCode(loginInfo.getCompanyCode());
        stPo.setOwnerCode(loginInfo.getOwnerCode());
        stPo.setOwnerParCode(loginInfo.getOwnerParCode());
        smsTemplateMapper.insert(stPo);
        return stPo.getSmsTemplateId();
    }

    @Override
    public void searchBySmsTemplateDefault(String smsTemplateType, Integer isDefault) {
        LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        List<Long> smsTemplateIds = smsTemplateMapper.searchBySmsTemplateDefault(smsTemplateType, BasedataDictConstants.IS_VALID, loginInfo.getAppId(), loginInfo.getOwnerCode());
        if (!CommonUtils.isNullOrEmpty(smsTemplateIds) && BasedataDictConstants.IS_VALID == isDefault) {
            throw new ServiceBizException("不能有重复的模板类型默认值!");
        }
    }

    @Override
    public void searchBySmsTemplateDefault(String smsTemplateType, Integer isDefault, Long smsTemplateId) {
        LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        List<Long> smsTemplateIds = smsTemplateMapper.searchBySmsTemplateDefault2(smsTemplateType, BasedataDictConstants.IS_VALID, smsTemplateId, loginInfo.getAppId(), loginInfo.getOwnerCode());
        if (!CommonUtils.isNullOrEmpty(smsTemplateIds)) {
            if (BasedataDictConstants.IS_VALID == isDefault) {
                throw new ServiceBizException("不能有重复的模板类型默认值!");
            }
        }
    }


    /**
     * 给模板信息设置属性
     *
     * @param stPo
     * @param stDto
     * @return void
     * @author dengqiang
     * @since 2019/9/19
     */
    @Override
    public void setSmsTemplate(SmsTemplatePO stPo, SmsTemplateDTO stDto){
        stPo.setSmsTemplateType(stDto.getSmsTemplateType());
        stPo.setSmsTemplateName(stDto.getSmsTemplateName());
        stPo.setSmsTemplateContent(stDto.getSmsTemplateContent());
        stPo.setIsDefault(stDto.getIsDefault());
        stPo.setDataSources(stDto.getDataSources());
    }

    /**
     * 修改短信模板信息
     *
     * @param stDto
     * @return java.lang.Long
     * @author dengqiang
     * @since 2019/9/19
     */
    @Override
    public Long updateSmsTemplate(SmsTemplateDTO stDto) {
        SmsTemplatePO stPo = smsTemplateMapper.selectById(stDto.getSmsTemplateId());
        searchBySmsTemplateDefault(stDto.getSmsTemplateType(),stDto.getIsDefault(),stDto.getSmsTemplateId());
        setSmsTemplate(stPo, stDto);
        //修改短信模板信息
        stPo.setRecordVersion(stDto.getRecordVersion());
        smsTemplateMapper.updateById(stPo);
        return stDto.getSmsTemplateId();
    }

    /**
     * 根据id查询短信模板
     *
     * @param smsTemplateId
     * @return com.yonyou.cyxdms.basedata.service.vo.smstemplate.SmsTemplateVO
     * @author dengqiang
     * @since 2019/9/19
     */
    @Override
    public SmsTemplateVO getSmsTemplateById(Long smsTemplateId) {
        SmsTemplateVO smsTemplateVo = new SmsTemplateVO();
        SmsTemplatePO smsTemplatePo = smsTemplateMapper.selectById(smsTemplateId);
        if(smsTemplatePo == null){
            return null;
        }
        BeanUtils.copyProperties(smsTemplatePo,smsTemplateVo);
        return smsTemplateVo;
    }

    /**
     * 短信模板删除
     *
     * @param smsTemplateId
     * @return void
     * @author dengqiang
     * @since 2019/9/19
     */
    @Override
    public void deleteSmsTemplate(Long smsTemplateId) {
        smsTemplateMapper.deleteById(smsTemplateId);
    }

    /**
     * 根据模板类型查询模板信息
     *
     * @param smsTemplateType
     * @return com.yonyou.cyxdms.basedata.service.vo.smstemplate.SmsTemplateVO
     * @author dengqiang
     * @since 2019/9/19
     */
    @Override
    public List<SmsTemplateVO> getBySmsTemplateType(String smsTemplateType) {
        LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        return smsTemplateMapper.getBySmsTemplateType(smsTemplateType,loginInfo.getAppId(),loginInfo.getOwnerCode());
    }

}
